﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace TextMIS
{
    public partial class frmInfoEdit : Form
    {
        public frmInfoEdit()
        {
            InitializeComponent();
        }
        public static string userName;
        public static string strSql;
        SqlConnection conn;
        SqlCommand com;
        SqlDataReader dr;

        private void frmInfoEdit_Load(object sender, EventArgs e)
        {
            conn = new
SqlConnection(frmLogin.SQLstr);

            cbxNewSex.Items.Add("男");
            cbxNewSex.Items.Add("女");

            userName = frmLogin.userName;
            strSql = "select * from PatientInfo  where Username = '" + userName + "'";
            com = new SqlCommand(strSql,conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

             dr = com.ExecuteReader();

            if (dr.Read())
            {
                oldName.Text = dr.GetValue(1).ToString();
                oldSex.Text = dr.GetValue(2).ToString();
                oldTel.Text = dr.GetValue(3).ToString();
                oldAddress.Text = dr.GetValue(4).ToString();
                txtNewName.Text = dr.GetValue(1).ToString();
                cbxNewSex.Text = dr.GetValue(2).ToString();
                txtNewTel.Text = dr.GetValue(3).ToString();
                txtNewAddress.Text = dr.GetValue(4).ToString();
            }
            conn.Close();
            txtNewName.Enabled = false;
            cbxNewSex.Enabled = false;
            txtNewTel.Enabled = false;
            txtNewAddress.Enabled = false;

        }

        private void btnRepeat_Click(object sender, EventArgs e)
        {
                txtNewName.Text = oldName.Text;
                cbxNewSex.Text = oldSex.Text;
                txtNewTel.Text = oldTel.Text;
                txtNewAddress.Text = oldAddress.Text;
            txtNewName.Enabled = false;
            cbxNewSex.Enabled = false;
            txtNewTel.Enabled = false;
            txtNewAddress.Enabled = false;

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            com = new SqlCommand("update PatientInfo set Pname = '" +
                   txtNewName.Text.Trim() + "',Psex = '" + cbxNewSex.Text.Trim() + "',Pphone = '" +
                   txtNewTel.Text.Trim() + "',Paddress = '" + txtNewAddress.Text.Trim() + "'where Username = '" + userName + "'", conn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmInfoEdit_Click(object sender, EventArgs e)
        {
            txtNewName.Enabled = true;
            cbxNewSex.Enabled = true;
            txtNewTel.Enabled = true;
            txtNewAddress.Enabled = true;
        }




    }
}
